package com.wangsd.web.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.MyController;
import com.wangsd.web.sys.entity.SysRole;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.service.SysRoleService;
import com.wangsd.web.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.wangsd.common.enums.ResultCode.NAME_REPEAT;
import static com.wangsd.common.enums.ResultCode.NO_USER;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-02
 */
@Api(value = "sysRole", tags = "角色管理")
@RestController
@RequestMapping("/sys/sysRole")
public class SysRoleController extends MyController {

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysUserService sysUserService;

    @ApiOperation(value = "查询所有角色", notes = "查询所有角色", httpMethod = "GET")
    @GetMapping("/selectAll/{type}")
    public Result selectAll(@ApiParam(value = "类型", required = true) @PathVariable Long type) {
        List<SysRole> list = sysRoleService.list(new QueryWrapper<SysRole>().eq("type",type));
        return Result.success(list);

    }

    @ApiOperation(value = "查询角色列表", notes = "查询角色列表", httpMethod = "POST")
    @PostMapping("/selectList/{pageNum}/{pageSize}")
    public Result selectList(@ApiParam(value = "当前页", required = true) @PathVariable Integer pageNum,
                             @ApiParam(value = "每页大小", required = true) @PathVariable Integer pageSize,
                             @RequestBody @ApiParam(name = "sysRole", value = "传入json格式") SysRole sysRole) {

        IPage<SysRole> sysUserIPage = sysRoleService.selectList(pageNum, pageSize, sysRole);
        return Result.success(sysUserIPage);

    }

    @ApiOperation(value = "根据用户查询拥有的角色id", notes = "根据用户查询拥有的角色id", httpMethod = "GET")
    @GetMapping("/selectRoleIdByUserId/{userId}")
    public Result selectRoleIdByUserId(@ApiParam(value = "用户id", required = true) @PathVariable Long userId) {
        List<SysRole> list = sysRoleService.selectByUserId(userId);
        List<Long> ids = list.stream().map(SysRole::getId).collect(Collectors.toList());
        return Result.success(ids);
    }

    @ApiOperation(value = "新增或修改角色信息", notes = "新增或修改角色信息", httpMethod = "POST", response = Result.class)
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody SysRole entity) {
        SysRole role = sysRoleService.selectByName(entity.getName(), entity.getId());
        if (role != null) {
            return Result.message(NAME_REPEAT);
        }
        Integer integer = sysRoleService.addOrUpdate(entity);
        return Result.success(integer);
    }

    @ApiOperation(value = "根据id删除角色", notes = "根据id删除角色", httpMethod = "GET")
    @GetMapping("/delete/{id}")
    public Result delete(@ApiParam(value = "用户id", required = true) @PathVariable Long id) {
        boolean b = sysRoleService.removeById(id);
        return Result.success(b);
    }
}

